//
//  HMWelcomeViewController.swift
//  01-SinaWeibo
//
//  Created by 周英培 on 16/5/24.
//  Copyright © 2016年 周英培. All rights reserved.
//

import UIKit

import SnapKit

import SDWebImage
/*
 1.用户头像和文案
 2.显示的时候 有动画
 */
class HMWelcomeViewController: UIViewController {

    //MARK:懒加载 文案 和 用户头像 北京图像
    //北京图像
    private lazy var backView = UIImageView(image: UIImage(named: "ad_background"))
    
    //用户头像
    private lazy var iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //文案
    private lazy var nameLabel : UILabel = UILabel(title: HMUserInfoViewModel.sharedUserInfoViewModel.userInfoModel?.name ?? "", font: 18, textColor: UIColor.darkGrayColor())
        
        
        
//        {
//    
//        let l = UILabel()
//        
//        //设置文字
//        l.text = HMUserInfoViewModel.sharedUserInfoViewModel.userInfoModel?.name
//        
//        print(HMUserInfoViewModel.sharedUserInfoViewModel.userInfoModel?.name)
//        
//        //设置文字尺寸
//        l.font = UIFont .systemFontOfSize(18)
//        
//        //设置文字颜色
//        l.textColor = UIColor.darkGrayColor()
//        
//        //设置自适应尺寸
//        l .sizeToFit()
//        
//        //设置透明度
//        l.alpha = 0
//        
//        return l
//    
//    }()
    
    override func loadView() {
        super.loadView()
        //设置北京图片
        view = backView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //加载UI
        setUpUI()
    }

    //MARK:加载UI
    func setUpUI() {
    view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        //设置约束
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.width.height.equalTo(90)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        //使用SDWebImage加载用户头像
        iconView.sd_setImageWithURL(HMUserInfoViewModel.sharedUserInfoViewModel.headURL)
        
        //设置切角
        iconView.cornerRadius = 45
        
        iconView.clipsToBounds = true
        
        nameLabel.alpha = 0
        
    
    }
   

    //MARK:实现动画效果 最好在willDidAppear里面实现
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        //开始动画
        startAnimation()
    }
    
    func startAnimation() {
    
        //弹簧动画效果
        //usingSpringWithDamping: 阻尼系数 0 ~ 1 值越小 效果越弹
        //initialSpringVelocity : 加速度 值越大 速度越快
        //OC:位移枚举 表示多个动画选项 '|' swift: 使用数组数组
        let offSet = -ScreenHeight + 160
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 2, options: [], animations: {
            
            //修改约束
            //            self.iconView.snp_makeConstraints(closure: { (make) in
            //                make.bottom.equalTo(-400)
            //            })
            //取消之前的所有约束 在重写添加约束
            //self.iconView.snp_remakeConstraints(closure: <#T##(make: ConstraintMaker) -> Void#>)
            
            //更新约束
            self.iconView.snp_updateConstraints(closure: { (make) in
                
                make.bottom.equalTo(offSet)
                
            })
            
            //刷新父视图
//            self.loadViewIfNeeded()
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
               UIView.animateWithDuration(2, animations: {
                
                self.nameLabel.alpha = 1
                
                }, completion: { (_) in
                    
                    print("ok")
                    
//                    UIApplication.sharedApplication().keyWindow?.rootViewController = HMMainViewController()
                    //发送通知
                    NSNotificationCenter.defaultCenter().postNotificationName(switchRootviewController, object: "main")
                    
               })
                
        }
        
    
    }
    
}
